package cn.pugle.base.net.socket.client;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * 网友的代码
 *下面这段复制来的代码读入了数据 其实也一直打印 READ
 * count = -1时应该清除这个channel
* */
public class NioNonBlockingSelectorTest {
    Selector selector;
    private ByteBuffer receivebuffer = ByteBuffer.allocate(1024);

    public static void main(String[] args) throws Exception {
        new NioNonBlockingSelectorTest().testNioNonBlockingSelector();
    }

    public void testNioNonBlockingSelector() throws Exception {
        selector = Selector.open();
        SocketAddress address = new InetSocketAddress(CONS.SERVER_PORT);
        ServerSocketChannel channel = ServerSocketChannel.open();
        channel.socket().bind(address);
        channel.configureBlocking(false);
        channel.register(selector, SelectionKey.OP_ACCEPT);
        while (true) {
            selector.select();
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                iterator.remove();
                handleKey(selectionKey);
            }
        }
    }

    private void handleKey(SelectionKey selectionKey) throws IOException {
        ServerSocketChannel server = null;
        SocketChannel client = null;
        if (selectionKey.isAcceptable()) {
            server = (ServerSocketChannel) selectionKey.channel();
            client = server.accept();
            System.out.println("客户端： " + client.socket().getRemoteSocketAddress().toString());
            client.configureBlocking(false);
            client.register(selector, SelectionKey.OP_READ);
        }
        if (selectionKey.isReadable()) {
            System.out.println("READ");
            client = (SocketChannel) selectionKey.channel();
            receivebuffer.clear();
            int count = client.read(receivebuffer);
            if (count > 0) {
                String receiveText = new String(receivebuffer.array(), 0, count);
                System.out.println("服务器端接受客户端数据--:" + receiveText);
//                client.register(selector, SelectionKey.OP_READ);
            }
        }
    }
}
